package org.lzuploader.gui.listeners.main;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Shell;
import org.lzuploader.controller.AccountsAccessBean;
import org.lzuploader.gui.WLogin;
import org.lzuploader.gui.WMain;
import org.lzuploader.gui.helpers.EasyMessageBox;
import org.lzuploader.model.Config;
import org.lzuploader.model.Localization;

/** Class used when the user clicks on a send widget.
 * 
 * @author Emmanuel Cron
 *
 */
public class SendSelectionListener implements SelectionListener
{
    /** Logger of this class. */
    private static Log log = LogFactory.getLog(SendSelectionListener.class);
    
    /** Localization of this class. */
    private Localization localization = Localization.getInstance();
    
    /** Used when widget is selected by default.
     * @param e Associated event
     */
    @Override
    public void widgetDefaultSelected(final SelectionEvent e)
    {
        widgetSelected(e);        
    }

    /** Used when widget is manually selected.
     * @param e Associated event
     */
    @Override
    public void widgetSelected(final SelectionEvent e)
    {
        log.debug("User has clicked on a send widget.");
        
        log.trace("Checking file size.");
        
        final Config config = Config.getInstance();
        final int size = AccountsAccessBean.getFileSize(config.getSelectedAccount());
        
        if (size < 1)
        {
            log.debug("File size is inferior to 1, returning.");
            
            EasyMessageBox.info(
                localization.getString("DIALOG_TITLE_INFO"),
                localization.getString("DIALOG_FILE_TOO_SMALL")
            );
            
            return;
        }
        
        log.trace("Displaying login window.");
        
        Shell wMainShell = null;
        for (int i = 0 ; i < e.display.getShells().length ; i++)
        {
            if (e.display.getShells()[i].getData() instanceof WMain)
            {
                wMainShell = e.display.getShells()[i];
                break;
            }
        }
        
        new WLogin(wMainShell);
    }
}
